A constant table (also known as a constant pool) is a data structure that stores fixed values in a program. For example, in the context of the Java Virtual Machine, the values stored in the constant pool can represent values that do not charge over the course of execution such as literals (such as strings, integers, double, floats, etc.), field references, method references, interface references, name/type information, and others. One use of the constant table is to provide information regarding the programmer's intent for constant values to the run-time environment executing the program.
The run-time environment can then use that knowledge to create instances of those constant values in static memory for use by the program. Since the constant values are stored in a location where they cannot be changed, the run-time environment is free to reuse those constant values when needed to avoid re-instantiating the constant value for each use (or in some cases the need to track the value for garbage collection). For example, assume the source code of a program defines two variables which both refer to the string literal “hello world”. In some run-time environments, such as the Java Virtual Machine, without the use of constants the environment would need to instantiate “hello world” twice and then assign each variable to the memory location of its respective string. However, if the string “hello world” is a constant, the operating environment is free to instead create one instantiation of “hello world” and assign both variables to the same reference. Since the “hello world” string is immutable in memory, there is no risk that an operation might use the first variable to modify the “hello world” string and cause an unintended cross-over effect to the second variable. If an instruction attempts to modify the first variable, a second instantiation representing the modified string is created and the reference stored by the first variable is shifted to the location of the modified string. Thus, constant values serve as an important part of optimizing the execution of programs by conserving memory and preventing work from being needlessly duplicated.
At present, constant tables and the process of loading constants from those constant tables are tailored to specific types of values that are hard coded or supported natively by the run-time environment. These techniques allow the developer of the run-time environment to fine-tune the handling of those constants and develop optimizations specific to a particular type of value. However, the run-time efficiency that can be obtained from utilizing constant values is applicable to virtually any type of value (such as complex numbers, tuples, points, groups, etc.). One issue with expanding the concept of constant values to other types is that it is virtually impossible for the designer of the run-time environment (such as a virtual machine executing the program) to anticipate every type of constant value that could possibly be useful to software developers. Thus, a flexible approach to representing constant values in the constant table and making those constant values available in run-time memory would be a significant advancement in the pursuit of expressive and efficiently executed programs.